from django.core import exceptions
from rest_framework import serializers

from api.models import Book, Press, User


class PressModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Press
        fields = ('press_name', 'pic', 'address')


class BookListSerializer(serializers.ListSerializer):

    def update(self, instance, validated_data):
        print(instance)
        print(validated_data)
        print(self.child)

        for index, obj in enumerate(instance):
            self.child.update(obj, validated_data[index])
        return instance


class BookDeModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = ('book_name', 'price', 'publish', 'authors')
        list_serializer_class = BookListSerializer
        extra_kwargs = {
            'book_name': {
                'required': True,
                'min_length': 2,
                'error_messages': {
                    'required': '图书名必须提供',
                    'min_length': '图书名不能少于2个字符',
                }
            }
        }

    def validate(self, attrs):
        return attrs

    def validate_book_name(self, data):
        return data


class BookModelSerializerV2(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = ("book_name", "price", "publish", "authors", "pic1")
        extra_kwargs = {
            "book_name": {
                "required": True,
                "min_length": 2,
                "error_messages": {
                    "required": "图书名必须提供",
                    "min_length": "图书名不能少于两个字符",
                }
            },
            "pic": {
                "read_only": True
            },
            "publish": {
                "write_only": True
            },
            "authors": {
                "write_only": True
            },
        }

    def validate(self, attrs):
        return attrs

    def validate_book_name(self, data):
        if Book.objects.filter(book_name=data):
            raise exceptions.ValidationError
        return data


class UserModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('user_name', 'user_pwd')

        extra_kwargs = {
            'user_name': {
                "required": True,
                "min_length": 6,
                "error_messages": {
                    "required": "用户名必须提供",
                    "min_length": "用户名不能少于6个字符",
                }
            },
            'user_pwd': {
                "required": True,
                "min_length": 6,
                "error_messages": {
                    "required": "密码必须提供",
                    "min_length": "密码不能少于6个字符",
                }
            }
        }


class UserModelSerializerV2(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('user_name', 'user_pwd')

        extra_kwargs = {
            'user_name': {
                "required": True,
                "min_length": 6,
                "error_messages": {
                    "required": "用户名必须提供",
                    "min_length": "用户名不能少于6个字符",
                }
            },
            'user_pwd': {
                "required": True,
                "min_length": 6,
                "error_messages": {
                    "required": "密码必须提供",
                    "min_length": "密码不能少于6个字符",
                }
            }
        }

    def validate_user_name(self, data):
        if User.objects.filter(user_name=data):
            raise exceptions.ValidationError
        return data
